package com.marvels.approve.common.service.hystrix;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.marvels.approve.common.service.ISysUserService;
import com.marvels.approve.process.dto.SysUserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.system.permission.service.SysUserService;
/**获取用户级联角色的熔断
 * @author 何杰
 * 2018年5月10日
 */
@Service
public class SysUserServiceHystrixClientFallBack implements ISysUserService{

	@Autowired
	private SysUserService sysUserService;
	
	@Override
	public SysUserDTO querySysUser(Integer userId) {
		try {
			com.marvels.system.permission.dto.SysUserDTO dto = new com.marvels.system.permission.dto.SysUserDTO();
			dto.setUserId(userId);
			com.marvels.system.permission.dto.SysUserDTO apiResult = sysUserService.getUserById(dto);
			SysUserDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), SysUserDTO.class);
			MarvelsLogUtil.getInstance().info("ISysUserService.querySysUser direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ISysUserService.querySysUser error", e);
		}
		return null;
	} 
	
	public SysUserDTO querySysUserByUserCode(String userCode) {
		try {
			com.marvels.system.permission.dto.SysUserDTO apiResult = sysUserService.getUserByUserCode(userCode);
			SysUserDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), SysUserDTO.class);
			MarvelsLogUtil.getInstance().info("ISysUserService.querySysUserByUserCode direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ISysUserService.querySysUserByUserCode error", e);
		}
		return null;
	}
	
	@Override
	public List<SysUserDTO> getUserList() throws MarvelsBusinessException {
		try {
			Page<com.marvels.system.permission.dto.SysUserDTO> sysUserList = sysUserService.selectUserList();
			List<SysUserDTO> result = JSONObject.parseArray(JSONObject.toJSONString(sysUserList), SysUserDTO.class);
			MarvelsLogUtil.getInstance().info("ISysUserService.getUserList direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ISysUserService.getUserList error", e);
		}
		return null;
	}
	
	@Override
	public List<SysUserDTO> selectUserByRoleCode(String roleCode) throws MarvelsBusinessException {
		try {
			List<com.marvels.system.permission.dto.SysUserDTO> sysUserList = sysUserService.selectUserByRoleCode(roleCode);
			List<SysUserDTO> result = JSONObject.parseArray(JSONObject.toJSONString(sysUserList), SysUserDTO.class);
			MarvelsLogUtil.getInstance().info("ISysUserService.selectUserByRoleCode direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ISysUserService.selectUserByRoleCode error", e);
		}
		return null;
	}
}
